/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.profile;

import org.openaecfm.model.profile.LShapeProfileDef;
import org.openaecfm.model.profile.ParameterizedProfileDef;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.RealParameter;
import org.openaecfm.spf.model.SimpleEntityInstance;

public final class LShapeProfileDefIO extends
		AbstractEntityInstanceInputImpl<LShapeProfileDef> implements
		IEntityInstanceOutput<LShapeProfileDef> {

	private IEntityInstanceUtilInput<ParameterizedProfileDef> utilInput;
	private IEntityInstanceUtilOutput<ParameterizedProfileDef> utilOutput;

	public void setUtilOutput(
			IEntityInstanceUtilOutput<ParameterizedProfileDef> utilOutput) {
		this.utilOutput = utilOutput;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String exportSPF(LShapeProfileDef pojo, IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance(
				"IFCLSHAPEPROFILEDEF", appDataManager.getName(pojo));

		utilOutput.exportSPF(pojo, ei, appDataManager);

		// Set Depth
		ei.addParameter(new RealParameter(pojo.getDepth()));

		// Set Width
		if (pojo.getWidth() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getWidth()));
		}

		// Set Thickness
		ei.addParameter(new RealParameter(pojo.getThickness()));

		// Set FilletRadius
		if (pojo.getFilletRadius() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getFilletRadius()));
		}

		// Set EdgeRadius
		if (pojo.getEdgeRadius() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getEdgeRadius()));
		}

		// Set LegSlope
		if (pojo.getLegSlope() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getLegSlope()));
		}

		return ei.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected LShapeProfileDef doCreateObject() {
		return new LShapeProfileDef();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveReferenceParameter(EntityInstance ei, LShapeProfileDef pojo,
			IAppDataManager appDataManager) {
		utilInput.resolveReferenceParameter(ei, pojo, appDataManager);
	}

	public void setUtilInput(
			IEntityInstanceUtilInput<ParameterizedProfileDef> utilInput) {
		this.utilInput = utilInput;
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei,
			LShapeProfileDef pojo) {
		utilInput.resolveNotReferenceParameter(ei, pojo);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set Depth
		RealParameter depth = (RealParameter) sei.getParameter(3);
		pojo.setDepth(depth.toDouble());

		// Set Width
		Parameter width = sei.getParameter(4);
		if (width.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) width;
			pojo.setWidth(r.toDouble());
		}

		// Set Thickness
		RealParameter thickness = (RealParameter) sei.getParameter(5);
		pojo.setThickness(thickness.toDouble());

		// Set FilletRadius
		Parameter filletradius = sei.getParameter(6);
		if (filletradius.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) filletradius;
			pojo.setFilletRadius(r.toDouble());
		}

		// Set EdgeRadius
		Parameter edgeradius = sei.getParameter(7);
		if (edgeradius.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) edgeradius;
			pojo.setEdgeRadius(r.toDouble());
		}

		// Set LegSlope
		Parameter legslope = sei.getParameter(8);
		if (legslope.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) legslope;
			pojo.setLegSlope(r.toDouble());
		}
	}
}
